
import {
	mapState,
	mapMutations
} from "vuex"
import {
	getShoppingCartList,
	getAddressBookDefault,
	orderPay
} from "./api.js"
export default {
	data() {
		return {
			goodsList: [],
			defaultAddress: null
		}
	},
	onShow() {
		this.getOrderDataList();
		this.getDefaultAddress();
	},
	onLoad(options) {
		
	},
	computed:{
		getTotalPrice:function (){
			let p = 0.0;
			this.goodsList.forEach((element, index, array) => {
				p = p + element.amount;
			});
			return p;
		},
		getTotalNum: function(){
			let n = 0;
			this.goodsList.forEach((element, index, array) => {
				n = n + element.number;
			});
			return n;
		}
	},
	methods: {
		orderPay(){
			let body = '支付订单'
			let params = {
				body: body,
				addressBookId: this.defaultAddress.id,
			}
			orderPay(params).then( async (res)=>{
				if(res.code === 200){
					// 这里支付成功
					// const [err, payRes] = await uni.requestPayment({
					// 	...res.data
					// });
					// const [err, payRes] = null
					// 这里跳到支付成功页去
					uni.navigateTo({
						url: '/pages/success/index' 
					})
					// if(err){
					// 	uni.showToast({
					// 		title: "支付失败!",
					// 		icon: "none",
					// 	})
					// 	// 这里跳到订单详情页去
					// 	uni.navigateTo({
					// 		url: '/pages/product/index' 
					// 	})
					// }else{
						
					// }
				}else{
					uni.showToast({
						title: "订单提交失败！",
						icon: "none",
					})
					this.goBack();
				}
			}).catch((error)=>{})
		},
		// 提交订单事件
		submitOrder(){
			uni.navigateTo({
				url: '/pages/pay/index?orderId=1' 
			})
		},
		getDefaultAddress(){
			getAddressBookDefault().then((res) =>{
				if(res.code === 200){
					this.defaultAddress = res.data;
				}else{
					uni.showToast({
						title: "默认地址查询失败！",
						icon: "none",
					})
				}
			}).catch((error) =>{})
		},
		changeAddress(){
			uni.navigateTo({
				url: "/pages/address/index",
			})
		},
		getOrderDataList(){
			getShoppingCartList().then( (res)=>{
				this.goodsList = res.data;
			}
			).catch((error) =>{})
		},
		// 返回上一级
		goBack() {
			uni.navigateBack({ delta: 1 })
		},
	},

}


		
		